#include "list.h"
#include "ui_list.h"
#include <QTreeWidgetItem>
#include <QMessageBox>

List::List(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::List)
{
    ui->setupUi(this);

    connect(ui->addButton, SIGNAL(clicked()),
            this, SLOT(addItem()));
    connect(ui->delButton, SIGNAL(clicked()),
            this, SLOT(delItem()));
    connect(ui->upButton, SIGNAL(clicked()),
            this, SLOT(moveItemUp()));
    connect(ui->downButton, SIGNAL(clicked()),
            this, SLOT(moveItemDown()));
}

List::~List()
{
    delete ui;
}

void List::setColumnCount(int count)
{
    m_cols = count;
}

void List::setHeader(const QStringList &header)
{
    ui->treeWidget->setHeaderLabels(header);
}

void List::setData(const QString &data)
{
    QStringList splitted = data.split(';');
    int rows = splitted.count() / m_cols;
    for (int row = 0; row < rows; ++row) {
        QStringList rowData;
        for (int i = row * m_cols; i < row * m_cols + m_cols; ++i)
            rowData << splitted.at(i);

        QTreeWidgetItem *item = new QTreeWidgetItem(rowData);
        item->setFlags(item->flags() | Qt::ItemIsEditable);
        ui->treeWidget->insertTopLevelItem(row, item);
    }
}

QString List::data() const
{
    int cols = ui->treeWidget->header()->count();
    int rows = ui->treeWidget->topLevelItemCount();
    QStringList result;

    for (int row = 0; row < rows; ++row) {
        QTreeWidgetItem *item = ui->treeWidget->topLevelItem(row);
        for (int col = 0; col < cols; ++col)
            result << item->text(col);
    }
    return result.join(";");
}

void List::addItem()
{
    int columns = ui->treeWidget->header()->count();
    QStringList strings;
    for (int i = 0; i < columns; ++i)
        strings << "*";

    int idx = ui->treeWidget->topLevelItemCount();
    if (ui->treeWidget->currentIndex().isValid())
        idx = ui->treeWidget->currentIndex().row() + 1;

    QTreeWidgetItem *item = new QTreeWidgetItem(strings);
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    ui->treeWidget->insertTopLevelItem(idx, item);
}

void List::delItem()
{
    QModelIndex index = ui->treeWidget->currentIndex();
    if (index.isValid()) {
        QTreeWidgetItem *item = ui->treeWidget->takeTopLevelItem(index.row());
        delete item;
    } else {
        QMessageBox::warning(this, tr("Warning"), tr("You must select an item to delete"));
    }
}

void List::moveItemUp()
{
    moveItem(-1);
}

void List::moveItemDown()
{
    moveItem(1);
}

void List::moveItem(int delta)
{
    QModelIndex index = ui->treeWidget->currentIndex();
    if (index.isValid()) {
        int dest_row = index.row() + delta;
        if (dest_row < 0 || dest_row >= ui->treeWidget->topLevelItemCount()) {
            QMessageBox::warning(this, tr("Warning"), tr("Can't move item"));
            return;
        }

        QTreeWidgetItem *item = ui->treeWidget->takeTopLevelItem(index.row());
        ui->treeWidget->insertTopLevelItem(dest_row, item);
        ui->treeWidget->setCurrentItem(item);
    } else {
        QMessageBox::warning(this, tr("Warning"), tr("You must select an item to move"));
    }
}
